
 <!DOCTYPE HTML>
<html >
<head>
  <meta charset="UTF-8">
  
    <title>基于多线程实现通用搜索 | Typhoon&#39;s code space</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="typhoon">
    

    
    <meta name="description" content="说起搜索，大家心里边都有一些概念，我们平时使用的百度和谷歌，其实就是给我们拱了搜索的能力， 当我们输入一个关键字点击搜索后，搜索引擎会返回给我们一大堆相关和不相关的内容。">
<meta name="keywords" content="通用搜索">
<meta property="og:type" content="article">
<meta property="og:title" content="基于多线程实现通用搜索">
<meta property="og:url" content="http://scorpioaeolus.oschina.io/2018/08/18/基于多线程实现通用搜索/index.html">
<meta property="og:site_name" content="Typhoon&#39;s code space">
<meta property="og:description" content="说起搜索，大家心里边都有一些概念，我们平时使用的百度和谷歌，其实就是给我们拱了搜索的能力， 当我们输入一个关键字点击搜索后，搜索引擎会返回给我们一大堆相关和不相关的内容。">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/08/18/基于多线程实现通用搜索/s1.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/08/18/基于多线程实现通用搜索/s2.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/08/18/基于多线程实现通用搜索/s3.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/08/18/基于多线程实现通用搜索/s4.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/08/18/基于多线程实现通用搜索/s5.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/08/18/基于多线程实现通用搜索/s6.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/08/18/基于多线程实现通用搜索/s7.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/08/18/基于多线程实现通用搜索/s8.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/08/18/基于多线程实现通用搜索/s9.png">
<meta property="og:updated_time" content="2018-08-18T11:04:06.508Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="基于多线程实现通用搜索">
<meta name="twitter:description" content="说起搜索，大家心里边都有一些概念，我们平时使用的百度和谷歌，其实就是给我们拱了搜索的能力， 当我们输入一个关键字点击搜索后，搜索引擎会返回给我们一大堆相关和不相关的内容。">
<meta name="twitter:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/08/18/基于多线程实现通用搜索/s1.png">

    
    <link rel="alternative" href="/atom.xml" title="Typhoon&#39;s code space" type="application/atom+xml">
    
    
    <link rel="icon" href="/typhoon/img/java.png">
    
    
    <link rel="apple-touch-icon" href="/typhoon/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/typhoon/img/jacman.jpg">
    
    <link rel="stylesheet" href="/typhoon/css/style.css">
</head>

  <body>
    <header>
      
<div>
		
			<div id="imglogo">
				<a href="/typhoon/"><img src="/typhoon/img/java.png" alt="Typhoon&#39;s code space" title="Typhoon&#39;s code space"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/typhoon/" title="Typhoon&#39;s code space">Typhoon&#39;s code space</a></h1>
				<h2 class="blog-motto"></h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/typhoon/">主页</a></li>
					
						<li><a href="/typhoon/archives">归档</a></li>
					
						<li><a href="/typhoon/about">关于</a></li>
					
					<li>
 					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="Search" />
						<input type="hidden" name="q" value="site:scorpioaeolus.oschina.io">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/typhoon/2018/08/18/基于多线程实现通用搜索/" title="基于多线程实现通用搜索" itemprop="url">基于多线程实现通用搜索</a>
  </h1>
  <p class="article-author">By
       
		<a href="/typhoon/about" title="typhoon" target="_blank" itemprop="author">typhoon</a>
		
  <p class="article-time">
    <time datetime="2018-08-18T07:19:27.000Z" itemprop="datePublished"> Published 2018-08-18</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		
			<ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#背景描述"><span class="toc-number">1.</span> <span class="toc-text">背景描述</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#解决方案"><span class="toc-number">2.</span> <span class="toc-text">解决方案</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#技术实现"><span class="toc-number">3.</span> <span class="toc-text">技术实现</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#测试验证"><span class="toc-number">4.</span> <span class="toc-text">测试验证</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#总结"><span class="toc-number">5.</span> <span class="toc-text">总结</span></a></li></ol>
		
		</div>
		
		<pre><code>    说起搜索，大家心里边都有一些概念，我们平时使用的百度和谷歌，其实就是给我们拱了搜索的能力，
当我们输入一个关键字点击搜索后，搜索引擎会返回给我们一大堆相关和不相关的内容。
</code></pre><a id="more"></a>
<pre><code>    当然谷歌和百度搜索引擎的实现原理和只是推荐远比我们想象的复杂的多，也不在此篇的讨论范畴，
我们就聊一下基于业务场景的通用搜索。首先明确一个概念，一个成熟的系统或者网站都会有很人性化的
用户导航能力，但是有些场景操作链路比较长，会对一些用户造成困扰和时间上的浪费，当然他们会选择
一个快捷的搜索入口，然后检索到自己关注的内容进行操作。例如：一个医药管理系统，用户使用的时候
寻找一个类目或者详情可能会在左侧导航栏点击几次才能找到，那如果在首页显眼的地方放置一个通用搜
索框，用户输入关键字的时候系统查询出相似度比较高的相关类目，会很大程度上提高使用者的操作效率
；再比如 在一个电商平台，用户输入关键字后会返回相关的店铺和产品。
    也就是说，对于一个优秀的系统或者网站，通用搜索是一个必备的能力。接下来我们聊一下使用java
自带线程池实现通用搜索功能。
</code></pre><h5 id="背景描述"><a href="#背景描述" class="headerlink" title="背景描述"></a>背景描述</h5><pre><code>    在一个售后服务平台，会员信息、退款信息和订单信息是用户最关心也是查看最多的功能，传统的实
现方案是在左侧设置不同的菜单，针对不同的类目展示相应的信息列表，然后输入查询条件检索到具体的
数据，最后点击详情查看到详细的信息。这种实现方案没有问题，但是假如我很明确想查看一笔订单的详
细内容，那么我至少需要操作三步才能看到详情信息：
①找到类目对应的菜单点进去
②输入查询条件查询到相关数据
③点击详情查看详细信息
    确实操作链路有点长，对于内部系统来说可能影响不大，如果是一个大型网站，那么很可能因为操作
过于复杂导致用户量流失。
</code></pre><h5 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h5><pre><code>    我们编写系统和网站的最终目的是提供方便，提高效率和降低损耗，那么从一定程度上来说，能够让
程序完成的操作，千万不要让用户去操作。对于上述的问题是将繁杂的检索操作交给程序完成，并且把三
个步骤合成一个，也就是提供一个能力让用户输入一个关键信息由系统做分析检索并返回结果推荐给用户，
具体要做什么操作由用户自己决定:
</code></pre><img src="/typhoon/2018/08/18/基于多线程实现通用搜索/s1.png" alt="search" title="search">
<pre><code>    在上图用例交互很清晰的表达了，系统需要在页面提供一个通用搜索框，后台实现搜索能力，然后将
结果返回给用户，具体查看哪些信息由用户自己选择。也即是在系统首页提供搜索框如下图：
</code></pre><img src="/typhoon/2018/08/18/基于多线程实现通用搜索/s2.png" alt="search" title="search">
<h5 id="技术实现"><a href="#技术实现" class="headerlink" title="技术实现"></a>技术实现</h5><pre><code>    既然叫做通用搜索能力，从体验角度来说，无法忍受响应时间过长；也就是说从技术实现角度，串行
查询然后返回结果是不可行的，也就是说如果不引入外部插件，我们要考虑使用多线程并行查询然后由程
序调用处同意收口返回。jdk1.5引入了线程池，对并发提供了很好的支持，所以此处我们使用jdk自带线程
池来实现并发查询，具体技术方案如下图：
</code></pre><img src="/typhoon/2018/08/18/基于多线程实现通用搜索/s3.png" alt="search" title="search">
<pre><code>核心代码结构图如下：
</code></pre><img src="/typhoon/2018/08/18/基于多线程实现通用搜索/s4.png" alt="search" title="search">
<p><strong>1.控制器层</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Controller</span></div><div class="line"><span class="meta">@RequestMapping</span>(<span class="string">"common"</span>)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SearchController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Autowired</span></div><div class="line">    <span class="keyword">private</span> SearchManager searchManager;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line">     * 通用查询</div><div class="line">     * <span class="doctag">@param</span> request</div><div class="line">     * <span class="doctag">@param</span> response</div><div class="line">     */</div><div class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"/get_search"</span>,method = RequestMethod.GET)</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">commonSearch</span><span class="params">(HttpServletRequest request, HttpServletResponse response)</span> </span>&#123;</div><div class="line">        String key = request.getParameter(<span class="string">"key"</span>);</div><div class="line">        Operator operator = getOperator(request);</div><div class="line">        <span class="keyword">try</span> &#123;</div><div class="line">            List&lt;SearchResultDto&gt; list =  <span class="keyword">this</span>.searchManager.search(key,operator);</div><div class="line">            <span class="keyword">if</span>(<span class="keyword">null</span> == list || list.isEmpty()) &#123;</div><div class="line">                <span class="keyword">this</span>.putFailtResult(response,<span class="string">"没有查到结果"</span>);</div><div class="line">                <span class="keyword">return</span>;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">this</span>.putSuccessResult(response,list);</div><div class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</div><div class="line">            e.printStackTrace();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p><strong>2.适配器层</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 查询管理器</div><div class="line"> *</div><div class="line"> */</div><div class="line"><span class="meta">@Service</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SearchManager</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Autowired</span></div><div class="line">    <span class="keyword">private</span> List&lt;ISearch&gt; iSearchList;</div><div class="line"></div><div class="line">    ExecutorService executorService = Executors.newFixedThreadPool(<span class="number">10</span>);</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> List&lt;SearchResultDto&gt; <span class="title">search</span><span class="params">(String key, Operator operator)</span> </span>&#123;</div><div class="line">        List&lt;SearchResultDto&gt; list  = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">        <span class="keyword">if</span>(<span class="keyword">null</span> == iSearchList || iSearchList.isEmpty()) &#123;</div><div class="line">            <span class="keyword">return</span> list;</div><div class="line">        &#125;</div><div class="line">        List&lt;Future&lt;List&lt;SearchResultDto&gt;&gt;&gt; futureList = <span class="keyword">new</span> ArrayList&lt;&gt;(iSearchList.size());</div><div class="line">        iSearchList.forEach(iSearch -&gt; &#123;</div><div class="line">             futureList.add(<span class="keyword">this</span>.executorService.submit(<span class="keyword">new</span> SearchThread(key,operator,iSearch)));</div><div class="line">        &#125;);</div><div class="line"></div><div class="line">       futureList.forEach(future -&gt; &#123;</div><div class="line">           <span class="keyword">try</span> &#123;</div><div class="line">               List&lt;SearchResultDto&gt;results = future.get();</div><div class="line">               <span class="keyword">if</span>(<span class="keyword">null</span> != results &amp;&amp; !results.isEmpty()) &#123;</div><div class="line">                   list.addAll(results);</div><div class="line">               &#125;</div><div class="line">           &#125; <span class="keyword">catch</span> (Exception e) &#123;</div><div class="line">               e.printStackTrace();</div><div class="line">           &#125;</div><div class="line">       &#125;);</div><div class="line">        <span class="keyword">return</span> list;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p><strong>3.实现层</strong></p>
<pre><code>ISearch:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 查询接口</div><div class="line"> */</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">ISearch</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line">     * 查询</div><div class="line">     *</div><div class="line">     * <span class="doctag">@param</span> key</div><div class="line">     * <span class="doctag">@param</span> operator</div><div class="line">     * <span class="doctag">@return</span></div><div class="line">     */</div><div class="line">    <span class="function">List&lt;SearchResultDto&gt; <span class="title">search</span><span class="params">(String key, Operator operator)</span></span>;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>AbstractSearch:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 通用搜索抽象类</div><div class="line"> *</div><div class="line"> */</div><div class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbstractSearch</span> <span class="keyword">implements</span> <span class="title">ISearch</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> List&lt;SearchResultDto&gt; <span class="title">search</span><span class="params">(String key, Operator operator)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(isParameterIllegal(key,operator)) &#123;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> bizSearch(key,operator);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line">     * 具体业务查询</div><div class="line">     *</div><div class="line">     * <span class="doctag">@param</span> key</div><div class="line">     * <span class="doctag">@param</span> operator</div><div class="line">     * <span class="doctag">@return</span></div><div class="line">     */</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> List&lt;SearchResultDto&gt; <span class="title">bizSearch</span><span class="params">(String key, Operator operator)</span></span>;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line">     * 检查入参是否非法</div><div class="line">     *</div><div class="line">     * <span class="doctag">@param</span> key</div><div class="line">     * <span class="doctag">@param</span> operator</div><div class="line">     * <span class="doctag">@return</span></div><div class="line">     */</div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">boolean</span> <span class="title">isParameterIllegal</span><span class="params">(String key, Operator operator)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(<span class="keyword">null</span> == key || key.isEmpty()) &#123;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span>(<span class="keyword">null</span> == operator) &#123;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>UserSearchImpl:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 用户查询实现</div><div class="line"> *</div><div class="line"> */</div><div class="line"><span class="meta">@Service</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserSearchImpl</span> <span class="keyword">extends</span> <span class="title">AbstractSearch</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Autowired</span></div><div class="line">    <span class="keyword">private</span> UserService userService;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> List&lt;SearchResultDto&gt; <span class="title">bizSearch</span><span class="params">(String key, Operator operator)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(!StringUtils.isNumeric(key)) &#123;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">        &#125;</div><div class="line">        List&lt;SearchResultDto&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">        User user = <span class="keyword">this</span>.userService.queryByPK(Long.valueOf(key));</div><div class="line">        <span class="keyword">if</span>(<span class="keyword">null</span> == user) &#123;</div><div class="line">            <span class="keyword">return</span> list;</div><div class="line">        &#125;</div><div class="line">        list.add(ConvertUtils.convertSearchResultDto(user));</div><div class="line">        <span class="keyword">return</span> list;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>OrderSearchImpl:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 订单查询实现</div><div class="line"> *</div><div class="line"> */</div><div class="line"><span class="meta">@Service</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OrderSearchImpl</span> <span class="keyword">extends</span> <span class="title">AbstractSearch</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Autowired</span></div><div class="line">    <span class="keyword">private</span> OrderService orderService;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> List&lt;SearchResultDto&gt; <span class="title">bizSearch</span><span class="params">(String key, Operator operator)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(!StringUtils.isNumeric(key)) &#123;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">        &#125;</div><div class="line">        List&lt;SearchResultDto&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">        Order order = <span class="keyword">this</span>.orderService.queryByOrderId(Long.valueOf(key));</div><div class="line">        <span class="keyword">if</span>(<span class="keyword">null</span> == order) &#123;</div><div class="line">            <span class="keyword">return</span> list;</div><div class="line">        &#125;</div><div class="line">        list.add(ConvertUtils.convertSearchResultDto(order));</div><div class="line">        <span class="keyword">return</span> list;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>RefundSearchImpl:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 退款查询实现</div><div class="line"> *</div><div class="line"> */</div><div class="line"><span class="meta">@Service</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RefundSearchImpl</span> <span class="keyword">extends</span> <span class="title">AbstractSearch</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Autowired</span></div><div class="line">    <span class="keyword">private</span> RefundService refundService;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> List&lt;SearchResultDto&gt; <span class="title">bizSearch</span><span class="params">(String key, Operator operator)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(!StringUtils.isNumeric(key)) &#123;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">        &#125;</div><div class="line">        List&lt;SearchResultDto&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">       Refund refund = <span class="keyword">this</span>.refundService.queryByRefundId(Long.valueOf(key));</div><div class="line">        <span class="keyword">if</span>(<span class="keyword">null</span> == refund) &#123;</div><div class="line">            <span class="keyword">return</span> list;</div><div class="line">        &#125;</div><div class="line">        list.add(ConvertUtils.convertSearchResultDto(refund));</div><div class="line">        <span class="keyword">return</span> list;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h5 id="测试验证"><a href="#测试验证" class="headerlink" title="测试验证"></a>测试验证</h5><pre><code>启动应用，我们通过debug的方式验证执行步骤和结果：
</code></pre><img src="/typhoon/2018/08/18/基于多线程实现通用搜索/s5.png" alt="search" title="search">
<pre><code>进入控制器层：
</code></pre><img src="/typhoon/2018/08/18/基于多线程实现通用搜索/s6.png" alt="search" title="search">
<pre><code>进入适配器层：
</code></pre><img src="/typhoon/2018/08/18/基于多线程实现通用搜索/s7.png" alt="search" title="search">
<pre><code>此步骤中可以看到ISearch有三个实现，并发查到两条结果。看到页面响应：
</code></pre><img src="/typhoon/2018/08/18/基于多线程实现通用搜索/s8.png" alt="search" title="search">
<pre><code>格式化后：
</code></pre><img src="/typhoon/2018/08/18/基于多线程实现通用搜索/s9.png" alt="search" title="search">
<pre><code>    可以看到返回两条条数据，类型分别是退款和订单，根据关键字没有查到会员数据，也就
验证实现了通用搜索能力。
</code></pre><h5 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h5><pre><code>    通过上述一系列描述，我们根据真实的业务场实现了通用搜索的能力，解决了用户对系统的操作复杂度，从而也就实现了我们开发系统体校降本
的目的，并且该实现是一种插件式编程，如果业务上要支持一种新的业务查询，我们直接对ISearch增加一个实现类即可，完全不用其他任何配置，
但是每一种方案和技术实现都是一把双刃剑，我们使用jdk自带线程池实现通用搜索能力的最大弊端是，如果并发比较大可能导致系统启用的线程特别
多引起内存问题和人就会用部分用户查询出现排队延迟现象，具体使用过程中可以预先评估业务量来设置线程池大小以及调整jvm内存参数。
    希望通过此篇的描述加深各位看官对多线程以及jdk自带线程池的理解和认识，并且能够在实际开发过程中有比较好的应用和体验。
</code></pre>  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/typhoon/categories/案例分析/">案例分析</a>
</div>


  <div class="article-tags">
  
  <span></span> <a href="/typhoon/tags/通用搜索/">通用搜索</a>
  </div>

</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://scorpioaeolus.oschina.io/2018/08/18/基于多线程实现通用搜索/" data-title="基于多线程实现通用搜索 | Typhoon&#39;s code space" data-tsina="null" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/typhoon/2018/08/26/ThreadLocal如何实现线程隔离/" title="ThreadLocal如何实现线程隔离">
  <strong>上一篇：</strong><br/>
  <span>
  ThreadLocal如何实现线程隔离</span>
</a>
</div>


<div class="next">
<a href="/typhoon/2018/08/11/Collections工具类二分查找优化/"  title="jdk源码分析之Collections--二分查找优化">
 <strong>下一篇：</strong><br/> 
 <span>jdk源码分析之Collections--二分查找优化
</span>
</a>
</div>

</nav>

	


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
 
 <ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#背景描述"><span class="toc-number">1.</span> <span class="toc-text">背景描述</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#解决方案"><span class="toc-number">2.</span> <span class="toc-text">解决方案</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#技术实现"><span class="toc-number">3.</span> <span class="toc-text">技术实现</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#测试验证"><span class="toc-number">4.</span> <span class="toc-text">测试验证</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#总结"><span class="toc-number">5.</span> <span class="toc-text">总结</span></a></li></ol>
 
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  


  
<div class="categorieslist">
	<p class="asidetitle">Categories</p>
		<ul>
		
		  
			<li><a href="/typhoon/categories/dubbo/" title="dubbo">dubbo<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java性能优化/" title="java性能优化">java性能优化<sup>17</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java源码分析/" title="java源码分析">java源码分析<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/motan/" title="motan">motan<sup>2</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/spring/" title="spring">spring<sup>11</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springboot/" title="springboot">springboot<sup>28</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springmvc/" title="springmvc">springmvc<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/容器/" title="容器">容器<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/工具/" title="工具">工具<sup>7</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/性能优化/" title="性能优化">性能优化<sup>5</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/架构/" title="架构">架构<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/案例分析/" title="案例分析">案例分析<sup>13</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/缓存/" title="缓存">缓存<sup>4</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/面试题/" title="面试题">面试题<sup>1</sup></a></li>
		  
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			
				<li><a href="/typhoon/tags/springboot/" title="springboot">springboot<sup>28</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/java性能优化/" title="java性能优化">java性能优化<sup>9</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/限流/" title="限流">限流<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring5/" title="Spring5">Spring5<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/案例分析/" title="案例分析">案例分析<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/性能优化/" title="性能优化">性能优化<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring/" title="Spring">Spring<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo/" title="dubbo">dubbo<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/分页/" title="分页">分页<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/并发/" title="并发">并发<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/异常拦截/" title="异常拦截">异常拦截<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo分布式事务/" title="dubbo分布式事务">dubbo分布式事务<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/自带图片/" title="自带图片">自带图片<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/svn导出mvn项目/" title="svn导出mvn项目">svn导出mvn项目<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo过滤器/" title="dubbo过滤器">dubbo过滤器<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/排序性能对比/" title="排序性能对比">排序性能对比<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/ListenableFuture/" title="ListenableFuture">ListenableFuture<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/PropertyChangeListener/" title="PropertyChangeListener">PropertyChangeListener<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/jdbcTemplate高可用/" title="jdbcTemplate高可用">jdbcTemplate高可用<sup>1</sup></a></li>
			
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
        
          <li>
            
            	<a href="https://coderq.com" target="_blank" title="一个面向程序员交流分享的新一代社区">码农圈</a>
            
          </li>
        
          <li>
            
            	<a href="http://wuchong.me" target="_blank" title="Jark&#39;s Blog">Jark&#39;s Blog</a>
            
          </li>
        
    </ul>
</div>

  


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS</a>
</div>

  <div class="weiboshow">
  <p class="asidetitle">Weibo</p>
    <iframe width="100%" height="119" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=119&fansRow=2&ptype=1&speed=0&skin=9&isTitle=1&noborder=1&isWeibo=0&isFans=0&uid=null&verifier=b3593ceb&dpc=1"></iframe>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello ,I&#39;m a java coder. <br/>
			This is my blog,believe it or not.</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		
		
		
		
		
		
		
		
		
		<a href="mailto:ScorpioAeolus@163.com" target="_blank" class="icon-email" title="Email Me"></a>
		
	</div>
			
		

		<p class="copyright">
		Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Jacman">Jacman</a> © 2019 
		
		<a href="/typhoon/about" target="_blank" title="typhoon">typhoon</a>
		
		
		</p>
</div>
</footer>
    <script src="/typhoon/js/jquery-2.0.3.min.js"></script>
<script src="/typhoon/js/jquery.imagesloaded.min.js"></script>
<script src="/typhoon/js/gallery.js"></script>
<script src="/typhoon/js/jquery.qrcode-0.12.0.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
  
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else{
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  c.click(function(){
    ta.css('display', 'block').addClass('fadeIn');
  });
  o.click(function(){
    ta.css('display', 'none');
  });
  $(window).scroll(function(){
    ta.css("top",Math.max(140,320-$(this).scrollTop()));
  });
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<div class="hoverqrcode clearfix"></div>',
  '<a class="overlay" id="qrcode"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);

  $('.hoverqrcode').hide();

  var myWidth = 0;
  function updatehoverqrcode(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
    var qrsize = myWidth > 1024 ? 200:100;
    var options = {render: 'image', size: qrsize, fill: '#2ca6cb', text: url, radius: 0.5, quiet: 1};
    var p = $('.article-share-qrcode').position();
    $('.hoverqrcode').empty().css('width', qrsize).css('height', qrsize)
                          .css('left', p.left-qrsize/2+20).css('top', p.top-qrsize-10)
                          .qrcode(options);
  };
  $(window).resize(function(){
    $('.hoverqrcode').hide();
  });
  $('.article-share-qrcode').click(function(){
    updatehoverqrcode();
    $('.hoverqrcode').toggle();
  });
  $('.article-share-qrcode').hover(function(){}, function(){
      $('.hoverqrcode').hide();
  });
});   
</script>









<link rel="stylesheet" href="/typhoon/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/typhoon/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->



<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?e6d1f421bbc9962127a50488f9ed37d1";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>



<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="Back to Top"><img src="/typhoon/img/scrollup.png"/></a>
	</div>
	<script src="/typhoon/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

<!-- Tiny_search Begin -->

<!-- Tiny_search End -->

  </body>
</html>
